import * as $apis from '../apis/index.js'
import config from "@/config/index.config.js";

var jweixin = require('jweixin-module');  

export default {  
        //判断是否在微信中  
    isWechat:function(){  
        var ua = window.navigator.userAgent.toLowerCase();  
        if(ua.match(/micromessenger/i) == 'micromessenger'){  
            return true;  
        }else{  
            return false;  
        }  
    },  
        //初始化sdk配置  
    initJssdk:async function(callback ,url){  
		var targetUrl = encodeURIComponent(location.href.split('#')[0]);
        //服务端进行签名 ，可使用uni.request替换。 签名算法请看文档  
		let jsapiSignature
		let query ={targetUrl : targetUrl};
		let data = await $apis.jsapiSignature(query);
		jweixin.config({  
		     debug: false,  
		     appId: data.mp.appId,  
		     timestamp:data.mp.timestamp,  
		     nonceStr: data.mp.nonceStr,  
		     signature:data.mp.signature,  
		     jsApiList: [  
		         'checkJsApi',  
		         'onMenuShareTimeline',  
		         'onMenuShareAppMessage',
				 'updateAppMessageShareData',
				 'updateTimelineShareData'
		     ]  
		});  
		callback();
    }, 
	wxOrder:{
		
	},
	callPay:function(data,paySuccess){
		this.wxOrder = data.wxOrder;
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
               document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady, false);
            }else if (document.attachEvent){
               document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady);
               document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady);
            }
        }else{
           this.onBridgeReady(paySuccess);
       }
	},
	
	onBridgeReady:function(paySuccess){
       WeixinJSBridge.invoke(
           'getBrandWCPayRequest', {
                "appId":this.wxOrder.appId,     //公众号名称，由商户传入
                "paySign":this.wxOrder.paySign,         //微信签名
                "timeStamp":this.wxOrder.timeStamp, //时间戳，自1970年以来的秒数
                "nonceStr":this.wxOrder.nonceStr , //随机串
                "package":this.wxOrder.packageValue,  //预支付交易会话标识
                "signType":this.wxOrder.signType     //微信签名方式
            },
            function(res){
	            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
	            	paySuccess();
		        }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
		            uni.showToast({
		            	title: '支付撤销',
		            	duration: 1500,
		            	mask:false,
		            	icon:'none'
		            });
		        }else if(res.err_msg == "get_brand_wcpay_request:fail" ){
					alert('支付失败');
		        } 
            }
		);
	},
	 
    //在需要自定义分享的页面中调用  
    share:function(data ,url){  
        url =url ? url :window.location.href;  
        if(!this.isWechat()){  
            return ;  
        }  
        //每次都需要重新初始化配置，才可以进行分享  
        this.initJssdk(function(){  
            jweixin.ready(function(){
                var shareData = {  
                     title: data&&data.title ? data.title: `${config.loginTitleTxt}`,  
                     desc: data&&data.desc ? data.desc: `${config.descTxt}`,  
                     link: url,  
                     imgUrl: data&&data.img ?data.img :`${config.logoImgUrl}`,  
                     success: function (res) {  
                        //用户点击分享后的回调，这里可以进行统计，例如分享送金币之类的  
                     },  
                     cancel: function (res) {  
                     }  
                 };  
                 //分享给朋友接口  
                 jweixin.onMenuShareTimeline(shareData);  
                 //分享到朋友圈接口  
                 jweixin.onMenuShareAppMessage(shareData);  
            });  
        } ,url);  
    }  
}